<template>
	<div class="system-user-container app-container">
		<el-card shadow="always">
			<el-row :gutter="20">
				<!--部门数据-->
				<el-col :span="4" :xs="24">
					<div class="head-container">
						<el-input v-model="tableData.deptName" placeholder="请输入部门名称" clearable prefix-icon="el-icon-search" style="margin-bottom: 20px" />
					</div>
					<div class="head-container">
						<el-tree
							:data="tableData.deptOptions"
							:props="tableData.defaultProps"
							node-key="deptId"
							:expand-on-click-node="false"
							:filter-node-method="filterNode"
							ref="tree"
							default-expand-all
							@node-click="handleNodeClick"
						/>
					</div>
				</el-col>

				<el-col :span="20" :xs="24">
					<el-input size="default" placeholder="请输入用户名称" style="max-width: 180px"> </el-input>
					<el-button size="default" type="primary" class="ml10">
						<el-icon>
							<ele-Search />
						</el-icon>
						查询
					</el-button>
					<el-button size="default" type="success" class="ml10" @click="onOpenAddUser">
						<el-icon>
							<ele-FolderAdd />
						</el-icon>
						新增用户
					</el-button>
					<el-table :data="tableData.data" style="width: 100%">
						<el-table-column type="index" label="序号" width="60" />
						<el-table-column prop="username" label="用户名称" show-overflow-tooltip></el-table-column>
						<el-table-column prop="nickname" label="用户昵称" show-overflow-tooltip></el-table-column>
						<el-table-column prop="deptName" label="部门" show-overflow-tooltip></el-table-column>
						<el-table-column prop="mobile" label="手机号码" show-overflow-tooltip></el-table-column>
						<el-table-column prop="email" label="邮箱" show-overflow-tooltip></el-table-column>
						<el-table-column prop="status" label="用户状态" show-overflow-tooltip>
							<template #default="scope">
								<el-tag type="success" v-if="scope.row.status === 0">启用</el-tag>
								<el-tag type="info" v-else>禁用</el-tag>
							</template>
						</el-table-column>
						<el-table-column prop="describe" label="用户描述" show-overflow-tooltip></el-table-column>
						<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ formatPast(scope.row.createTime, 'YYYY-mm-dd') }}</span>
							</template>
						</el-table-column>
						<el-table-column label="操作" width="100">
							<template #default="scope">
								<el-button size="small" type="text" @click="onOpenEditUser(scope.row)">修改</el-button>
								<el-button size="small" type="text" @click="onRowDel(scope.row)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination
						@size-change="onHandleSizeChange"
						@current-change="onHandleCurrentChange"
						class="mt15"
						:pager-count="5"
						:page-sizes="[10, 20, 30]"
						v-model:current-page="tableData.param.pageNum"
						background
						v-model:page-size="tableData.param.pageSize"
						layout="total, sizes, prev, pager, next, jumper"
						:total="tableData.total"
					>
					</el-pagination>
				</el-col>
			</el-row>
		</el-card>
		<!-- <AddUser ref="addUserRef" />
		<EditUser ref="editUserRef" /> -->
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import AddUser from '/@/views/system/user/component/addUser.vue';
import EditUser from '/@/views/system/user/component/editUser.vue';
import { useDeptApi } from '/@/api/system/dept';
import { useUserApi } from '/@/api/system/user';
import { formatPast } from '/@/utils/formatTime';
import other from '/@/utils/other';

export default defineComponent({
	name: 'systemUser',
	// components: { AddUser, EditUser },
	setup() {
		const deptApi = useDeptApi();
		const userApi = useUserApi();
		const addUserRef = ref();
		const editUserRef = ref();
		const state = reactive({
			tableData: {
				// 部门树选项
				deptOptions: [],
				// 部门名称
				deptName: undefined,
				defaultProps: {
					children: 'children',
					label: 'name',
				},
				data: [],
				total: 0,
				// 日期范围
				dateRange: [],
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
					username: undefined,
					mobile: undefined,
					status: undefined,
					deptId: undefined,
				},
			},
		});
		// TODO 未完成
		// 初始化表格数据
		const initTableData = () => {
			deptApi.listSimpleDepts().then((response) => {
				if (response.data) {
					state.tableData.deptOptions = other.handleTree(response.data, 'id', 'parentId', 'children', 0);
				}
			});
			userApi
				.listUser(
					other.addDateRange(
						state.tableData.param,
						[
							state.tableData.dateRange[0] ? state.tableData.dateRange[0] + ' 00:00:00' : undefined,
							state.tableData.dateRange[1] ? state.tableData.dateRange[1] + ' 23:59:59' : undefined,
						],
						'undefined'
					)
				)
				.then((response) => {
					if (response.data) {
						state.tableData.data = response.data;
						state.tableData.total = response.data.total;
					}
				});
		};
		// 打开新增用户弹窗
		// const onOpenAddUser = () => {
		// 	addUserRef.value.openDialog();
		// };
		// // 打开修改用户弹窗
		// const onOpenEditUser = (row: any) => {
		// 	editUserRef.value.openDialog(row);
		// };
		// 删除用户
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.userName}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					ElMessage.success('删除成功');
				})
				.catch(() => {});
		};
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			addUserRef,
			editUserRef,
			// onOpenAddUser,
			// onOpenEditUser,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
			formatPast,
			...toRefs(state),
		};
	},
});
</script>
<style scoped lang="scss">
.system-user-container {
	.system-user-search {
		text-align: left;

		.system-user-search-btn {
			text-align: center;
			margin-left: 70px;
		}
	}

	.system-user-photo {
		width: 40px;
		height: 40px;
		border-radius: 100%;
	}
}
</style>
